package edu.uic.cs.cs582.q_wordnet.process.impl;

import org.apache.log4j.Logger;

import edu.uic.cs.cs582.data.Component;
import edu.uic.cs.cs582.data.Synset;
import edu.uic.cs.cs582.misc.LogHelper;
import edu.uic.cs.cs582.process.impl.LogicFormulaProcessorImpl;
import edu.uic.cs.cs582.q_wordnet.data.SynsetsPolarityStore;

public class QWordNetLogicFormulaProcessorImpl extends
		LogicFormulaProcessorImpl
{
	private static final Logger LOGGER = LogHelper
			.getLogger(QWordNetLogicFormulaProcessorImpl.class);

	@Override
	protected void initializeCnfGenerator(Component component)
	{
		super.initializeCnfGenerator(component);
		//		this.majoritySynsetsCnfGenerator = new QWordNetMajoritySynsetsCnfGeneratorImpl(
		//				component);
	}

	@Override
	protected int[] generateBasicCnfTermForEachSynset(int synsetIndex)
	{
		/**
		 * If we know the synset's polarity, its basic term would be TRUE anyway.
		 * The basic term means the synset's polarity is Positive, or Negative, or Neutral.
		 * And we can guarantee that each synset has only one polarity in Q-WordNet, 
		 * see {@link SynsetsPolarityStore}: 
		 * 	Assert.isTrue(original == null, "Duplicate Synset ID - " + synsetId);
		 */
		if (isSynsetPolarityAlreadyKnown(synsetIndex))
		{
			return null;
		}

		return super.generateBasicCnfTermForEachSynset(synsetIndex);
	}

	private boolean isSynsetPolarityAlreadyKnown(int synsetIndex)
	{
		Synset synset = component.getSynsetByIndex(synsetIndex);
		String synsetId = synset.getId();

		boolean result = SynsetsPolarityStore.getInstance().isPolarityKnown(
				synsetId);
		if (result && LOGGER.isDebugEnabled())
		{
			LOGGER.debug("Synset["
					+ synsetId
					+ "]'s polarity is already known, so the basic terms will be ignored. ");
		}

		return result;
	}

}
